package com.feijiang.controller;


import com.feijiang.entity.ExpeData;
import com.feijiang.service.IExpeDataService;
import com.feijiang.util.ResponseData;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@RestController
@RequestMapping("/expe-data")
public class ExpeDataController {

  @Resource
  IExpeDataService iExpeDataService;

  @PostMapping("/add")
  public ResponseData add(@RequestParam(value = "expeId", required = true) int expeId, @RequestParam(value = "creatorId", required = true) int creatorId, MultipartFile file) {
    Assert.notNull(file, "未选择文件");
    boolean b = iExpeDataService.add(expeId, creatorId, file);
    Assert.isTrue(b, "上传失败");
    return ResponseData.success("上传成功", true);
  }

  @PostMapping("/delete/{id}")
  public ResponseData deleteById(@PathVariable("id") int id) {
    Assert.isTrue(iExpeDataService.deleteById(id), "删除失败");
    return ResponseData.success("删除成功", true);
  }

  @PostMapping("/deleteByExpeId/{id}")
  public ResponseData deleteByExpeId(@PathVariable("id") int expeId) {
    Assert.isTrue(iExpeDataService.deleteByExpeId(expeId), "删除失败");
    return ResponseData.success("删除成功", true);
  }



  @GetMapping("/listByExpeId")
  public ResponseData queryByPage(@RequestParam(defaultValue = "10") int pageSize,
      @RequestParam(defaultValue = "1") int pageNum, @RequestParam(required = true) int expeId) {
    Assert.isTrue(pageNum > 0 && pageSize > 0, "请输入正确的参数");
    List<ExpeData> expeDataList = iExpeDataService.queryByPage(pageSize, pageNum, expeId);
    return ResponseData.success(expeDataList);

  }

  /**
   * 根据id获取所有被审核的数据集
   * @param expeId
   * @return
   */
  @GetMapping("/listAll")
  public ResponseData listAll(@RequestParam(required = true) int expeId) {
    return ResponseData.success(iExpeDataService.listAll());
  }

  /**
   * 分页获取没有审核的
   * @param pageSize
   * @param pageNum
   * @return
   */
  @GetMapping("/list/unchecked")
  public ResponseData listUnchecked(@RequestParam(defaultValue = "10") int pageSize,
      @RequestParam(defaultValue = "1") int pageNum) {
    return ResponseData.success(iExpeDataService.listUnchecked(pageSize, pageNum));
  }

  @GetMapping("listAllByPage")
  public ResponseData listAllByPage(@RequestParam(defaultValue = "10") int pageSize,
      @RequestParam(defaultValue = "1") int pageNum) {
    Assert.isTrue(pageNum > 0 && pageSize > 0, "请输入正确的参数");
    return ResponseData.success(iExpeDataService.listAllByPage(pageSize, pageNum));
  }

  @PostMapping("/check/{id}")
  public ResponseData checkById(@PathVariable("id")int id) {
    return ResponseData.success("审核通过", iExpeDataService.checkById(id));
  }

  @GetMapping("/download/{name}")
  public void downloadByName(@PathVariable("name")String name, HttpServletResponse response)
      throws IOException {
    byte[] bytes = iExpeDataService.downloadByName(name);

      response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
      response.setHeader("Content-Disposition",
          "attachment; filename=" + name);

    ServletOutputStream outputStream = response.getOutputStream();
    outputStream.write(bytes);
    outputStream.flush();
    outputStream.close();
  }


}
